Imports Phoenix.DataAccess
Imports Phoenix.Forms

Namespace Helpers

    Public Class ProductHelper : Inherits BaseHelper

#Region " Edit Commands "
        Public Shared Function Copy(ByVal obj As DataSetData.Product) As DataSetData.Product
            Dim s As DataSetData.Product = AppDataLayer.Products.NewProduct
            s = Copy(s, obj)
            s.Label = "Copy of " & s.Label
            Return s
        End Function

        Private Shared Function Copy(ByVal s As DataSetData.Product, ByVal obj As DataSetData.Product) As DataSetData.Product
            s.Label = obj.Label
            s.Type = obj.Type
            s.CriticalStock = obj.CriticalStock
            s.Family = obj.Family
            s.Price = obj.Price
            s.Stock = obj.Stock
            Return s
        End Function

        Public Shared Sub DeleteProduct(ByVal obj As DataSetData.Product)
            DeleteProduct(obj, True)
        End Sub

        Public Shared Sub DeleteProduct(ByVal obj As DataSetData.Product, ByVal confirm As Boolean)
            If CloseForm(obj) Then
                If confirm Then
                    If Not AppConfig.SilenceProductDeletion Then
                        Dim result As MsgBoxResult = AppMessageBox(String.Format(Res.ProductDeleteQuestion, obj.Label), MsgBoxStyle.YesNo Or MsgBoxStyle.Exclamation)
                        If result <> MsgBoxResult.Yes Then Return
                    End If
                End If

                DeleteProductCore(obj)
            End If
        End Sub


        Public Shared Sub DeleteProducts(ByVal list As IList)
            If list.Count = 1 Then
                DeleteProduct(list(0), True)
            Else
                DeleteProducts(list, True)
            End If
        End Sub

        Public Shared Sub DeleteProducts(ByVal list As IList, ByVal confirm As Boolean)
            If (Not AppConfig.SilenceProductsDeletion) And confirm Then
                Dim result As MsgBoxResult = AppMessageBox(String.Format(Res.ProductsDeleteQuestion, list.Count), MsgBoxStyle.YesNo Or MsgBoxStyle.Exclamation)
                If result <> MsgBoxResult.Yes Then Return
            End If

            OperationCanceled = False
            Dim mylist(list.Count - 1) As DataSetData.Product
            list.CopyTo(mylist, 0)
            For i As Long = 0 To list.Count - 1
                DeleteProduct(mylist(i), False)
                If OperationCanceled Then Return
            Next
        End Sub

        Public Shared Sub UpdateProduct(ByVal product As DataSetData.Product)
            UpdateProductCore(product)
        End Sub

#End Region

#Region " CRUD Methods "
        Private Shared Sub UpdateProductCore(ByVal Product As DataSetData.Product)
            If Product.RowState = DataRowState.Detached Then
                AppDataLayer.Products.AddProduct(Product)
            End If
            AppDataLayer.UpdateProduct(Product)
        End Sub

        Private Shared Sub DeleteProductCore(ByVal Product As DataSetData.Product)
            Product.Delete()
            AppDataLayer.UpdateProduct(Product)
        End Sub
#End Region

    End Class
End Namespace
